﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class EditEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    int id = Convert.ToInt16(Request.QueryString["id"]);
                    idLabel.Text = id.ToString();

                    Employee aEmployee = new EmployeeManager().GetEmployeeById(id);
                    if (aEmployee != null)
                    {
                        nameTextBox.Text = aEmployee.Name;
                        fatherNameTextBox.Text = aEmployee.FatherName;
                        motherNameTextBox.Text = aEmployee.MotherName;
                        presentAddressTextBox.Text = aEmployee.PresentAaddres;
                        permanentAddressTextBox.Text = aEmployee.PresentAaddres;
                        nationalIdTextBox.Text = aEmployee.NationalIdNumber;
                        emailTextbox.Text = aEmployee.Email; 
                        contactNoTextBox.Text=aEmployee.ContactNumber;

                    }


                }
                updateButton.Visible = false;
            }
        }

        protected void yesButton_Click(object sender, EventArgs e)
        {
            ClientScriptManager CSM = Page.ClientScript;
            if (!ReturnValue())
            {
                string strconfirm = "<script>if(!window.confirm('Are you sure?')){window.location.href='UpdateCompanyPage.aspx'}</script>";
                CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false);
            }

            yesButton.Visible = false;
            updateMessageLabel.Visible = false;
            updateButton.Visible = true;
            updateButton.Enabled = true;
            
        }


        private bool ReturnValue()
        {
            return false;
        }

        protected void updateButton_Click(object sender, EventArgs e)
        {
            EmployeeManager manager=new EmployeeManager();
            Employee aEmployee = new Employee();
            try
            {

                aEmployee.Id = Convert.ToInt32(idLabel.Text);
                aEmployee.Name = nameTextBox.Text;
                aEmployee.FatherName = fatherNameTextBox.Text;
                aEmployee.MotherName = motherNameTextBox.Text;
                aEmployee.PresentAaddres = presentAddressTextBox.Text;
                aEmployee.PermanentAaddres = permanentAddressTextBox.Text;
                aEmployee.Email = emailTextbox.Text;
                aEmployee.ContactNumber = contactNoTextBox.Text;
               
                if (manager.UpdateEmployeeInformation(aEmployee))
                {
                    messageLabel.ForeColor = Color.Green;
                    messageLabel.Text = "EmployeeInformation Updated Successfully";
                    updateButton.Enabled = false;

                }

            }
            catch (FormatException exception)
            {
                messageLabel.ForeColor = Color.Red;
                messageLabel.Text = "Contact No Must be Numeric!";
            }

            catch (Exception exception)
            {

                messageLabel.ForeColor = Color.Red;
                messageLabel.Text = exception.Message;
            }


            //Response.Redirect("ViewEmployeeInformation.aspx");
        }
    }
}